package com.zcg.springbootredis.controller;

import com.zcg.springbootredis.entity.Student;
import io.netty.handler.ssl.SslClientHelloHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * PROJECT_NAME: springboot-redis
 * ClassName: StudentHandler
 * Package: com.zcg.springbootredis.controller
 * Description:
 * 控制器：实现对Student的增、删、改、查
 *
 * @Author CaigaoZhang
 * @Flag Effort, Struggle !
 * @Create 2025-05-20 15:04:50 星期二
 * @Version 1.0
 */
@RestController
public class StudentHandler {

    /**
     * 用于操作Redis数据库
     * 项目启动时自动生成RedisTemplate实例化对象
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增数据(存数据)
     * @param student
     */
    @PostMapping("/set")
    // @RequestBody 将客户端传过来的数据转成Java对象
    public void set(@RequestBody Student student) {
        redisTemplate.opsForValue().set("student", student);
    }

    /**
     * 查询数据(取数据)
     * @param key
     * @return
     */
    @GetMapping("/get/{key}")
    public Student get(@PathVariable("key") String key) {
        return (Student)redisTemplate.opsForValue().get(key);
    }

    /**
     * 删除数据
     * @param key
     * @return
     */
    @DeleteMapping("/delete/{key}")
    public boolean delete(@PathVariable("key") String key) {
        redisTemplate.delete(key);
        // hasKey()：判断key值是否存在
        return redisTemplate.hasKey(key);
    }

    /**
     * 字符串类型处理数据
     * @return
     */
    @GetMapping("/string")
    public String stringTest() {
        redisTemplate.opsForValue().set("str","Hello Redis");
        String str = (String) redisTemplate.opsForValue().get("str");
        return str;
    }

    /**
     * 列表类型处理数据
     * @return
     */
    @GetMapping("/list")
    public List<String> listTest() {
        ListOperations<String, String> listOperations = redisTemplate.opsForList();
        //存数据
        listOperations.leftPush("list", "Hello");
        listOperations.leftPush("list", "Hello");
        listOperations.leftPush("list", "Redis");
        listOperations.leftPush("list", "Redis");
        listOperations.leftPush("list", "World");
        listOperations.leftPush("list", "World");
        //取数据
        List<String> list = listOperations.range("list", 0, 5);
        return list;
    }

    /**
     * 集合类型处理数据（value值会去重）
     * @return
     */
    @GetMapping("/set")
    public Set<String> setTest() {
        SetOperations<String, String> setOperations = redisTemplate.opsForSet();
        //存数据
        setOperations.add("set", "Hello");
        setOperations.add("set", "Hello");
        setOperations.add("set", "World");
        setOperations.add("set", "World");
        setOperations.add("set", "Java");
        setOperations.add("set", "Java");
        //取数据
        Set<String> set = setOperations.members("set");
        return set;
    }

    /**
     * 有序集合处理数据（value值会去重，通过第三个参数值定义顺序）
     * @return
     */
    @GetMapping("/zset")
    public Set<String> zsetTest() {
        ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
        // 存数据
        zSetOperations.add("zset", "Hello",1);
        zSetOperations.add("zset", "Hello",2);
        zSetOperations.add("zset", "World",3);
        zSetOperations.add("zset", "World",4);
        zSetOperations.add("zset", "Java",5);
        zSetOperations.add("zset", "Java",6);
        // 取数据
        Set<String> set = zSetOperations.range("zset", 0, 5);
        return set;
    }

    @GetMapping("/hash")
    public void hashTest() {
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        hashOperations.put("key", "hashKey", "hello");
        System.out.println(hashOperations.get("key","hashKey"));
    }
}
